package com.mcxx.modules.miniSalvation.order.service.read;


import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.family.dto.ExInsuranceDetailsDto;
import com.mcxx.modules.miniSalvation.family.dto.ExInsuranceExcelDto;
import com.mcxx.modules.miniSalvation.order.dao.read.BusiInsureDetailReadDao;
import com.mcxx.modules.miniSalvation.order.dao.read.OrderReadDao;
import com.mcxx.modules.miniSalvation.order.dao.write.BusiInsureDetailWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.ExInsuranceDrawUpDTO;
import com.mcxx.modules.miniSalvation.order.param.InsuranceParam;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.core.restservice.resultbean.*;
import com.yinhai.ta404.core.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@TaTransactional
@Service
@Slf4j
public class BusiInsureDetailReadService extends BaseService {


    @Resource
    private BusiInsureDetailReadDao insuranceDrawUpReadDao;

    @Resource
    private BusiInsureDetailWriteDao exInsuranceDrawUpWriteDao;

    @Resource
    private ExportExcelService exportExcelService;

    @Autowired
    private FileService fileService;

    @Resource
    private TaskExportWriteService taskExportWriteService;

    @Resource
    private OrderReadDao orderReadDao;


    public Page getInsuranceOrg(InsuranceParam param, PageParam pageParam, UserAccountVo accountVo){
        int restFlag = 0;
        List<ExInsuranceDrawUpDTO> dto = new ArrayList<>();

        QueryParamUtil.setBaseParamValue(param,accountVo);

        insuranceDrawUpReadDao.beginPager(pageParam);
        param.setAreaCode(accountVo.getAreaCode());
        List<ExInsuranceDrawUpDTO> insuranceOrg = insuranceDrawUpReadDao.getInsuranceOrg(param);
        log.info("保险状态更新成功：{}",restFlag);



        return insuranceDrawUpReadDao.endPager(insuranceOrg);

    }



    public String getInsuranceType(String insuranceId){
        return insuranceDrawUpReadDao.getInsuranceType(insuranceId);
    }



    public ExInsuranceDrawUpDTO getInsuranceInfo(String insuranceId){
        return insuranceDrawUpReadDao.getInsuranceInfo(insuranceId);
    }


    public List<ExInsuranceExcelDto> getInsuranceExcelInfo(String areaCode){
        return insuranceDrawUpReadDao.getInsuranceExcelInfo(areaCode);
    }


    public List<ExInsuranceDetailsDto> getInsuranceDetailsExcelInfo(InsuranceParam param){
       return orderReadDao.getIncludeObjs(param,new Date(),new Date());
    }



    public ExcelInfo exportInsuranceExcel(String exportType,InsuranceParam param,UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);

        if("1".equals(exportType)){




            param.setAreaCode(accountVo.getAreaCode());
            List<ExInsuranceExcelDto> list = getInsuranceExcelInfo(accountVo.getAreaCode());
            excelInfo = exportExcelService.exportOrgOpLogs(ExInsuranceExcelDto.class,list,"保险信息.xls",
                    "保险信息列表","保险信息列表",false,"insureCompany","insuranceEfficientTime",
                    "insureQuote","insureMembers","sumPrice","insureStatus");
        }

        if(null != excelInfo){
            InputStream sbs = new ByteArrayInputStream(excelInfo.getFile());
            String objectkey = fileService.upload(sbs,excelInfo.getFileName());
            if(StringUtils.isNotBlank(objectkey)){
                TaskExportEntity taskExport = new TaskExportEntity();
                taskExport.setId(UUIDUtils.getUUID());
                taskExport.setUserid(accountVo.getUserId());
                taskExport.setTaskName(excelInfo.getFileName());
                taskExport.setTaskTitle("保险信息列表");
                taskExport.setParam("xls");
                taskExport.setCreateTime(new Date());
                taskExport.setFileUrl(objectkey);
                taskExport.setState("02");
                int i = taskExportWriteService.insertIgnoreNull(taskExport);
                if(i > 0){
                    excelInfo = null;
                }
            }
        }
        return excelInfo;


    }



    public ExcelInfo exportDetailsExcel(String exportType, List<String> orderIdList,InsuranceParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);


        if("1".equals(exportType)){




            param.setAreaCode(accountVo.getAreaCode());
            List<ExInsuranceDetailsDto> list = getInsuranceDetailsExcelInfo(param);
            excelInfo = exportExcelService.exportOrgOpLogs(ExInsuranceDetailsDto.class,list,"已纳入保险对象信息.xls",
                    "已纳入保险对象信息列表","已纳入保险对象息列表",false,"street","community",
                    "name","idcard","standType","supplyType","selfcarLevel");
        }

        if(null != excelInfo){
            InputStream sbs = new ByteArrayInputStream(excelInfo.getFile());
            String objectkey = fileService.upload(sbs,excelInfo.getFileName());
            if(StringUtils.isNotBlank(objectkey)){
                TaskExportEntity taskExport = new TaskExportEntity();
                taskExport.setId(UUIDUtils.getUUID());
                taskExport.setUserid(accountVo.getUserId());
                taskExport.setTaskName(excelInfo.getFileName());
                taskExport.setTaskTitle("已纳入保险对象信息列表");
                taskExport.setParam("xls");
                taskExport.setCreateTime(new Date());
                taskExport.setFileUrl(objectkey);
                taskExport.setState("02");
                int i = taskExportWriteService.insertIgnoreNull(taskExport);
                if(i > 0){
                    excelInfo = null;
                }
            }
        }
        return excelInfo;
    }

}
